package com.lu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lu.constants.BlogStatusConstants;
import com.lu.constants.ConfigConstants;
import com.lu.constants.HttpStatusEnum;
import com.lu.dto.AjaxPutPage;
import com.lu.dto.AjaxResultPage;
import com.lu.dto.Result;
import com.lu.pojo.BlogCategory;
import com.lu.pojo.BlogInfo;
import com.lu.pojo.BlogTag;
import com.lu.pojo.BlogTagRelation;
import com.lu.service.BlogCategoryService;
import com.lu.service.BlogInfoService;
import com.lu.utils.DateUtils;
import com.lu.utils.ResultGenerator;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 博客分类 前端控制器
 * </p>
 *
 * @author xiaolu
 * @since 2020-05-31
 */
@Controller
@RequestMapping("/admin")
public class CategoryController {

    @Autowired
    private BlogCategoryService blogCategoryService;

    @Autowired
    private BlogInfoService blogInfoService;

    // 文章分类(列出所有未删除的文章)
    @GetMapping("/v1/category/list")
    @ResponseBody
    public Result<List<BlogCategory>> list(){
        QueryWrapper<BlogCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogCategory::getIsDeleted, BlogStatusConstants.ZERO);
        List<BlogCategory> list = blogCategoryService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)){
            ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);
        }
        return ResultGenerator.getResultByHttp(HttpStatusEnum.OK,list);
    }

    // 跳转到分类列表页面
    @GetMapping("/v1/category")
    public String gotoBlogCategory(){
        return "admin/category-list";
    }

    // 列表分类分页
    @ResponseBody
    @GetMapping("/v1/category/paging")
    public AjaxResultPage<BlogCategory> paing(AjaxPutPage<BlogCategory> ajaxPutPage, BlogCategory condition){

        QueryWrapper<BlogCategory> queryWrapper = new QueryWrapper<>(condition);
        queryWrapper.lambda()
                .orderByAsc(BlogCategory::getCategoryRank)
                .ne(BlogCategory::getCategoryId,1);
        Page<BlogCategory> page = ajaxPutPage.putPageToPage();
        blogCategoryService.page(page,queryWrapper);
        AjaxResultPage<BlogCategory> result = new AjaxResultPage<>();
        result.setData(page.getRecords());
        result.setCount(page.getTotal());
        return result;
    }

    // 修改列表分类状态
    @PostMapping("/v1/category/update")
    @ResponseBody
    public Result<String> upadte(BlogCategory blogCategory){

        // 通过id得到博客分类信息
        BlogCategory category = blogCategoryService.getById(blogCategory.getCategoryId());
        // 查看博客名字是否相同
        boolean flag = category.getCategoryName().equals(blogCategory.getCategoryName());
        if (flag){
            blogCategoryService.updateById(blogCategory);
        }else {
            BlogInfo blogInfo = new BlogInfo()
                    .setBlogCategoryId(blogCategory.getCategoryId())
                    .setBlogCategoryName(blogCategory.getCategoryName());
            UpdateWrapper<BlogInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(BlogInfo::getBlogCategoryId, blogCategory.getCategoryId());
            blogInfoService.update(blogInfo, updateWrapper);
            blogCategoryService.updateById(blogCategory);
        }
        return ResultGenerator.getResultByHttp(HttpStatusEnum.OKK);
    }

    // 修改分类状态
    @ResponseBody
    @PostMapping("/v1/category/isDel")
    public Result<String> isDel(BlogCategory blogCategory){

        boolean flag = blogCategoryService.updateById(blogCategory);
        if (flag){
            return ResultGenerator.getResultByHttp(HttpStatusEnum.OKK);
        }
        return ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);
    }

    // 跳转到添加文章分类页面
    @GetMapping("/v1/category/add")
    public String add(){

        return "admin/category-add";
    }

    // 提交添加的文章分类
    @PostMapping("/v1/category/add")
    @ResponseBody
    public Result<String> add1(BlogCategory blogCategory){

        // 设置创建时间
        blogCategory.setCreateTime(DateUtils.getLocalCurrentDate());
        // 添加新的文章分类
        boolean flag = blogCategoryService.save(blogCategory);
        if (flag){
            return ResultGenerator.getResultByHttp(HttpStatusEnum.OKKK);
        }
        return ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);
    }

    // 清除文章分类信息
    @PostMapping("/v1/category/clear")
    @ResponseBody
    public Result<String> clear(BlogCategory blogCategory){

        UpdateWrapper<BlogInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(BlogInfo::getBlogCategoryId, blogCategory.getCategoryId())
                .set(BlogInfo::getBlogCategoryId, ConfigConstants.DEFAULT_CATEGORY.getConfigField())
                .set(BlogInfo::getBlogCategoryName, ConfigConstants.DEFAULT_CATEGORY.getConfigName());
        boolean flag = blogInfoService.update(updateWrapper);
                flag = blogCategoryService.removeById(blogCategory.getCategoryId());
        if (flag){
            return ResultGenerator.getResultByHttp(HttpStatusEnum.OKK);
        }
        return ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);
    }


}

